﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace milanov
{
    //Eden
    public partial class Checkout : Page
    {
        Decimal cost;
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Session["Cart"] == null)
                Response.Redirect("ShoppingCart.aspx");
            BindBasket();
            if (Request.QueryString["add"] != null)
            {
                //Zet de waarde dat in de URL staat in een string variable
                int id = Convert.ToInt32(Request.QueryString["add"]);
                //laad de bijhorende methode en geef deze string mee
                Add(id);
            }
        }
        void Add(int id)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["fotosDB"].ConnectionString))
            {
                string query = "SELECT FileID, Filename, Price FROM [Fotos] WHERE FileID = @ID";
                using (SqlDataAdapter da = new SqlDataAdapter(query, con))
                {
                    //Zet de category parameter aan @cat zodat dit in de query wordt gezet
                    da.SelectCommand.Parameters.AddWithValue("@ID", id);
                    DataView view = (DataView)BasketData.Select(DataSourceSelectArguments.Empty);
                    var costQuery = view.Cast<DataRowView>().Select(drv => drv.Row.Field<decimal>("Price"));
                    cost = costQuery.Sum();
                    Basket.DataSource = view;
                    Basket.DataBind();
                }
            }
        }
        protected void RemoveProduct_Click(object sender, EventArgs e)
        {
            var cart = (List<string>)Session["Cart"];
            var removedProducts = Basket.Rows.Cast<GridViewRow>()
              .Where(row => ((CheckBox)row.FindControl("RemovedProducts")).Checked)
              .Select(row => Basket.DataKeys[row.RowIndex].Value.ToString()).ToList();
            cart.RemoveAll(removedProducts.Contains);
            BindBasket();
        }

        protected void BindBasket()
        {
            var sql = "SELECT FileID, Filename, Price FROM Fotos WHERE FileID IN ({0})";
            var values = (List<string>)Session["Cart"];
            if (values.Count > 0)
            {
                var parms = values.Select((s, i) => "@p" + i.ToString()).ToArray();
                var inclause = string.Join(",", parms);
                BasketData.SelectCommand = string.Format(sql, inclause);
                BasketData.SelectParameters.Clear();
                for (var i = 0; i < parms.Length; i++)
                {
                    BasketData.SelectParameters.Add(parms[i].Replace("@", ""), values[i]);
                }

                DataView view = (DataView)BasketData.Select(DataSourceSelectArguments.Empty);
                var costQuery = view.Cast<DataRowView>().Select(drv => drv.Row.Field<decimal>("Price"));
                cost = costQuery.Sum();
                Basket.DataSource = view;
                Basket.DataBind();
            }
        }

        protected void Basket_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Literal total = (Literal)e.Row.FindControl("TotalPrice");
                total.Text = cost.ToString("c");
            }
        }
    }
}